package com.qinge.blog.utils.exception;

import com.qinge.blog.utils.result.JsonResult;
import io.swagger.models.Model;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

/**
 * @author QDW
 * @date 2022/5/5 10:42
 * @description ServiceRuntimeException处理类
 **/
@RestControllerAdvice
class ServiceExceptionHandler {

    /**
     * 应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器
     * @param binder
     */
    @InitBinder
    public void initWebBinder(WebDataBinder binder){

    }

    /**
     * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
     * @param model
     */
    @ModelAttribute
    public void addAttribute(Model model) {

    }


    /**
     * 捕获ServiceException
     * @param exception
     * @return
     */
    @ExceptionHandler({com.qinge.blog.utils.exception.ServiceException.class})
    @ResponseStatus(HttpStatus.OK)
    public JsonResult serviceExceptionHandler(ServiceException exception) {
        return new JsonResult(exception);
    }

}
